﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.Services;
using DataMongo;
using DataMongo.Entities;
using DotNetOpenAuth.AspNet;
using ExtendedMongoMembership;
using Google.Contacts;
using Google.GData.Contacts;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using MVCMongo.Filters;


namespace MVCMongo.Controllers
{
    //[Authorize]
    // [InitializeSimpleMembership]
    public class AccountController : Controller
    {
        //
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login(string returnUrl, string provider)
        {
            var data = new LoginModel();
      
            data.Provider = provider;
            if (!string.IsNullOrEmpty(provider))
            {
                if (provider == "google")
                {
                    ViewBag.Header = "Login by Google Account";

                }
                if (provider == "yahoo")
                {
                    ViewBag.Header = "Login by Yahoo Account";
                    return RedirectToAction("YahooAPI","Contact");



                }
            }
            else
            {
                ViewBag.Header = "Login";
            }
            ViewBag.ReturnUrl = returnUrl;
            return View(data);
        }

        [HttpPost]
        public ActionResult LoginPost(LoginViewModel model, List<string> EmailAddress)
        {

         ;

            // Check if user already exists
            if (ModelState.IsValid)
            {

                // Add User
                var connection = new RepositoryBaseMongo("Users");
               
                WebSecurity.CreateUserAndAccount(model.LoginModel.UserName, model.LoginModel.Password);
                var userId = WebSecurity.GetUserId(model.LoginModel.UserName);
                List<Users> list = (from f in connection.GetAll<Users>()
                                    where f.UserId == userId
                                    select f).ToList();
                Users user = new Users();
                if (list.Count > 0) user = list[0];
                user.Provider = model.LoginModel.Provider;
                user.FirstName = model.LoginModel.FirstName;
                user.LastName = model.LoginModel.LastName;
                connection.InsertOrUpdate(user);
                // Add User Eamil
                var connection2 = new RepositoryBaseMongo("UserEmails");
                var userEmail = new UserEmails();
                int Id = 0;
                var userEmaillist = connection2.GetAll<UserEmails>().ToList();
                if (userEmaillist.Any())
                    Id = userEmaillist.Max(x => x.UserEmailId);
                Id += 1;
                userEmail.UserEmailId = Id;

                userEmail.UserId = userId;
                userEmail.Source = model.LoginModel.Provider;
                userEmail.UserEmail = model.LoginModel.UserName;

                connection2.InsertOrUpdate(userEmail);
               
                // Add User Email's Contact
                if (EmailAddress.Any())
                {
                    foreach (var item in EmailAddress)
                    {
                        var connection1 = new RepositoryBaseMongo("UserEmailContacts");
                      
                        var contacts = new UserEmailContacts();
                        contacts.UserEmailId = userEmail.UserEmailId;
                        contacts.EmailAddress = item;
                        connection1.InsertOrUpdate(contacts);
                    }

                }



                WebSecurity.Login(model.LoginModel.UserName, model.LoginModel.Password, false);
                // OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                // OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
              
                var userName = model.LoginModel.FirstName;
                return RedirectToRoute("Profile", new { userName = userName });
            }
            else
            {
                ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
            }
            return null;
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (!string.IsNullOrEmpty(model.Provider))
            {
                var connection = new RepositoryBaseMongo("Users");
                var data = connection.GetAll<Users>();
                var user = data.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());

                // Check if user already exists
                if (user == null)
                {

                    if (model.Provider == "google")
                    {
                        var viewModel = new LoginViewModel();
                        GoogleContactService.InitializeService(model.UserName, model.Password);
                        viewModel.LoginModel = model;


                        ViewBag.Header = "Login by Google";
                        viewModel.ContactDetails = GoogleContactService.GetAllContact();
                        return View("RegisterWithContact", viewModel);
                    }
                }


                //WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                //WebSecurity.Login(model.UserName, model.Password, false);


                //return RedirectToAction("index", "Home");
            }
           


            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                var connection = new RepositoryBaseMongo("Users");
                var data = connection.GetAll<Users>();
                var user = data.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
       
                return RedirectToAction("Index","Home");
            }

            //List<Users> list = (from f in connection.GetAll<Users>()
            //                                where f.UserName == model.UserName && f.Password == model.Password
            //                                select f).ToList();
            //if (list.Any())
            //{
            //    WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe);
            //    return RedirectToLocal(returnUrl);
            //}

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }

        //
        // POST: /Account/LogOff

       // [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            WebSecurity.Logout();

            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Account/Register

        [AllowAnonymous]
        public ActionResult Register()
        {
           // var connection = new RepositoryBaseMongo("User");
            return View();
        }

        //
        // POST: /Account/Register

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Register(RegisterModel model, string dob_month, string dob_day, string dob_year)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    var date = Convert.ToDateTime(dob_month + "/" + dob_day + "/" + dob_year).AddDays(1);
                    
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

                    WebSecurity.Login(model.UserName, model.Password);
                    var connection = new RepositoryBaseMongo("Users");
                    var userId = WebSecurity.GetUserId(model.UserName);
                    List<Users> list = (from f in connection.GetAll<Users>()
                                        where f.UserId == userId
                                        select f).ToList();
                    Users user = new Users();
                    if (list.Count > 0) user = list[0];
                    user.FirstName = model.FirstName;
                    user.LastName = model.LastName;
                    user.Gender = model.Gender;
                    user.BirthDay = date;
                    connection.InsertOrUpdate(user);
                    var userName = model.FirstName;
                    return RedirectToAction("Index","Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // POST: /Account/Disassociate

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Disassociate(string provider, string providerUserId)
        {
            string ownerAccount = OAuthWebSecurity.GetUserName(provider, providerUserId);
            ManageMessageId? message = null;

            // Only disassociate the account if the currently logged in user is the owner
            if (ownerAccount == User.Identity.Name)
            {
                // Use a transaction to prevent the user from deleting their last login credential
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
                {
                    bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
                    if (hasLocalAccount || OAuthWebSecurity.GetAccountsFromUserName(User.Identity.Name).Count > 1)
                    {
                        OAuthWebSecurity.DeleteAccount(provider, providerUserId);
                        scope.Complete();
                        message = ManageMessageId.RemoveLoginSuccess;
                    }
                }
            }

            return RedirectToAction("Manage", new { Message = message });
        }

        //
        // GET: /Account/Manage

        public ActionResult Manage(ManageMessageId? message)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
                : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
                : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
                : "";
            ViewBag.HasLocalPassword = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
            ViewBag.ReturnUrl = Url.Action("Manage");
            return View();
        }

        //
        // POST: /Account/Manage

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Manage(LocalPasswordModel model)
        {
            bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
            ViewBag.HasLocalPassword = hasLocalAccount;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (hasLocalAccount)
            {
                if (ModelState.IsValid)
                {
                    // ChangePassword will throw an exception rather than return false in certain failure scenarios.
                    bool changePasswordSucceeded;
                    try
                    {
                        changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
                    }
                    catch (Exception)
                    {
                        changePasswordSucceeded = false;
                    }

                    if (changePasswordSucceeded)
                    {
                        return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
                    }
                    else
                    {
                        ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                    }
                }
            }
            else
            {
                // User does not have a local password so remove any validation errors caused by a missing
                // OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    try
                    {
                        WebSecurity.CreateAccount(User.Identity.Name, model.NewPassword);
                        return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("", String.Format("Unable to create local account. An account with the name \"{0}\" may already exist.", User.Identity.Name));
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // POST: /Account/ExternalLogin

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult ExternalLogin(string provider, string returnUrl)
        {
            return new ExternalLoginResult(provider, Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
        }

        //
        // GET: /Account/ExternalLoginCallback

        [AllowAnonymous]
        public ActionResult ExternalLoginCallback(string returnUrl)
        {
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
            if (!result.IsSuccessful)
            {
                return RedirectToAction("ExternalLoginFailure");
            }

            if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
            {
                return RedirectToLocal(returnUrl);
            }

            if (User.Identity.IsAuthenticated)
            {
                // If the current user is logged in add the new account
                OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                // User is new, ask for their desired membership name
                string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
                ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
                // ContactsRequest cr = new ContactsRequest(result.Provider, result.ProviderUserId);
                ViewBag.ReturnUrl = returnUrl;

                if (result.Provider == "google")
                {
                    return View("ExternalLoginConfirmation", new RegisterExternalLoginModel
                    {
                        UserName = result.UserName,
                        ExternalLoginData = loginData,

                    });
                }
                return View("ExternalLoginConfirmation", new RegisterExternalLoginModel
                {
                    UserName = result.UserName,
                    ExternalLoginData = loginData,
                    FullName = result.ExtraData["name"],
                    Link = result.ExtraData["link"]
                });
            }
        }

        //
        // POST: /Account/ExternalLoginConfirmation

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("Users");
                var data = connection.GetAll<Users>();
                var user = data.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());

                // Check if user already exists
                if (user == null)
                {
                    // Insert name into the profile table
                    //db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                    // db.SaveChanges();
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password, false);
                    // OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                    // OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                    return RedirectToLocal(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                }
                return null;

                // // Insert a new user into the database
                //using (UsersContext db = new UsersContext())
                //{
                //    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                //    // Check if user already exists
                //    if (user == null)
                //    {
                //        // Insert name into the profile table
                //        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                //        db.SaveChanges();

                //        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                //        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                //        return RedirectToLocal(returnUrl);
                //    }
                //    else
                //    {
                //        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                //    }
                //}
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }

        //
        // GET: /Account/ExternalLoginFailure

        [AllowAnonymous]
        public ActionResult ExternalLoginFailure()
        {
            return View();
        }

        [AllowAnonymous]
        [ChildActionOnly]
        public ActionResult ExternalLoginsList(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return PartialView("_ExternalLoginsListPartial", OAuthWebSecurity.RegisteredClientData);
        }

        [ChildActionOnly]
        public ActionResult RemoveExternalLogins()
        {
            ICollection<OAuthAccount> accounts = OAuthWebSecurity.GetAccountsFromUserName(User.Identity.Name);
            List<ExternalLogin> externalLogins = new List<ExternalLogin>();
            foreach (OAuthAccount account in accounts)
            {
                AuthenticationClientData clientData = OAuthWebSecurity.GetOAuthClientData(account.Provider);

                externalLogins.Add(new ExternalLogin
                {
                    Provider = account.Provider,
                    ProviderDisplayName = clientData.DisplayName,
                    ProviderUserId = account.ProviderUserId,
                });
            }

            ViewBag.ShowRemoveButton = externalLogins.Count > 1 || OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
            return PartialView("_RemoveExternalLoginsPartial", externalLogins);
        }

        #region Helpers
        private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        public enum ManageMessageId
        {
            ChangePasswordSuccess,
            SetPasswordSuccess,
            RemoveLoginSuccess,
        }

        internal class ExternalLoginResult : ActionResult
        {
            public ExternalLoginResult(string provider, string returnUrl)
            {
                Provider = provider;
                ReturnUrl = returnUrl;
            }

            public string Provider { get; private set; }
            public string ReturnUrl { get; private set; }

            public override void ExecuteResult(ControllerContext context)
            {
                OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl);
            }
        }

        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // See http://go.microsoft.com/fwlink/?LinkID=177550 for
            // a full list of status codes.
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "User name already exists. Please enter a different user name.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "A user name for that e-mail address already exists. Please enter a different e-mail address.";

                case MembershipCreateStatus.InvalidPassword:
                    return "The password provided is invalid. Please enter a valid password value.";

                case MembershipCreateStatus.InvalidEmail:
                    return "The e-mail address provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidAnswer:
                    return "The password retrieval answer provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidQuestion:
                    return "The password retrieval question provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidUserName:
                    return "The user name provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.ProviderError:
                    return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                case MembershipCreateStatus.UserRejected:
                    return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
            }
        }
        #endregion

        public ActionResult YahooContactList(string oauth_token, string oauth_verifier)
        {
            var list = new GetYahooContacts() {OauthVerifier = oauth_verifier,OauthToken = oauth_token};
            var contact = list.GetContacts();
            return null;
        }
      
    }
}
